import 'dart:core';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'package:unt/viewmodel/counter_view_model.dart';
/*
1.创建自己需要共享的数据
2.在应用程序的顶层ChangeNotifierProvider
3.在其他位置使用共享的数据
 */
void main() {
  runApp(
    //第二步
      ChangeNotifierProvider(
        create: (ctx)=>Yjhviewmodel(),
        child: MaterialApp(home: TextHomePage()),
      )
  );
}
class Yjhconter extends InheritedWidget{
  //1.共享的数据
  final int count;
  //2.构造方法
  Yjhconter({required this.count,required Widget child}):super(child: child);
  //3.获取当前最近的Inherited对象
  static Yjhconter? of(BuildContext context){
    //沿着element树，去找到最近的YjhconterElement，从Element中取出Widget对象
    return context.dependOnInheritedWidgetOfExactType<Yjhconter>();
  }
  // 决定要不要回调State中的didchangeDependencies
  // 如果返回true：执行当前的InheritedWidget的State中的didchangeDependcies
  @override
  bool updateShouldNotify(Yjhconter oldWidget) {
    return oldWidget.count != this.count;
  }
}
class TextHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TextHomePageState();
  }
}

class TextHomePageState extends State<TextHomePage> {
  @override
  Widget build(BuildContext context) {
    //第三步
    int counter = Provider.of<Yjhviewmodel>(context).getcont;
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("listview"),
          ),
          body:Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  Yjhshowdata01(),
                  Yjhshowdata02()],
              ),
            ],
          ),
          floatingActionButton: Selector<Yjhviewmodel,Yjhviewmodel>(
              selector: (ctx,countvm)=>countvm,
              shouldRebuild: (prev,next)=>false,//要不要重新构建
              builder: (ctx,countvm,child){
                return FloatingActionButton(
                  onPressed: () {
                    setState(() {
                      countvm.count = counter+=1;
                    });
                  },
                  child:child,
                );
              },
              child: Icon(Icons.add)
          )
          //Consumer的用法
          // Consumer<Yjhviewmodel>(
          //   builder: (ctx,countvm,child){
          //     return FloatingActionButton(
          //       onPressed: () {
          //         setState(() {
          //           countvm.count = counter+=1;
          //         });
          //       },
          //       child:child,
          //     );
          //   },
          //   child: Icon(Icons.add)
          // )
          ,
        )
    );
  }
}
//
class Yjhshowdata01 extends StatelessWidget {
  const Yjhshowdata01({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    int counter = Provider.of<Yjhviewmodel>(context).getcont;
    print(counter);
    return Card(
      color: Colors.green,
      child: Text("当前计数:${counter}"),
    );
  }
}
//
class Yjhshowdata02  extends StatelessWidget {
  const Yjhshowdata02 ({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    int counter = Provider.of<Yjhviewmodel>(context).getcont;
    print(counter);
    return Container(
      color: Colors.blue,
      child: Text("当前计数:${counter}"),
    );
  }
}



